<?php

namespace App\Http\Controllers\VideoAdmin;

use App\Http\Config\ErrorCode;
use App\Http\Controllers\Admin\BaseController;
use App\Models\video\BroadcasterModel;
use App\Models\video\CarouselModel;
use App\Models\video\CatModel;
use App\Models\video\VideoSettingModel;
use App\Models\video\VodVideoModel;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class CarouselController extends BaseController
{
    private $field = [
        "thumbnail_id" => "require", "url" => "", "start_time" => "require", "end_time" => "require", "type" => "1", "sort" => 0, "brief" => null
    ]; //填入数据库需要存储的表字段
    private $fieldMsg = ["thumbnail_id" => "轮播图", "url" => "地址", "start_time" => "开始时间戳", "end_time" => "结束时间戳"];
    private $data = [];

    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    public function getList(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->return_result($this->returnData);
        }
        $db = CarouselModel::query()->select(["*"])->where(["mall_id" => $request->mallData['id']]);
        if($request->startTime && $request->endTime){
            $startTime = is_numeric($request->startTime) ? $request->startTime : strtotime($request->startTime);
            $endTime = is_numeric($request->endTime) ? $request->endTime : strtotime($request->endTime);
            $db->where('start_time', '<=', $startTime);
            $db->where('end_time', '>=', $endTime);
        }
        if($request->brief){
            $db->where("brief", "like", "%{$request->brief}%");
        }
        $page = $request->page ?: 1;
        $pageSize = $request->pageSize ?: 10;
        $data['count'] = $db->count();
        $data['currentPage'] = $page;
        $data['totalPage'] = ceil(($data['count'])/$pageSize);
        $data['list'] = $db->skip(($page - 1) * $pageSize)->take($pageSize)->orderBy("sort", "desc")->orderBy("create_time", "desc")->get();
        if($data['list']){
            $data['list'] = CarouselModel::transferField($data['list']->toArray());
        }
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    // 更新状态
    public function updateStatus(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        VideoSettingModel::doUpdate(["carousel_status" => $request->status ?: 0], ["mall_id" => $request->mallData["id"]]);
        return $this->return_result($this->returnData);
    }

    // 添加
    public function create(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        foreach ($this->field as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if($v === 'require' && !$request->$ks){
                return ErrorCode::code("params_not_exist", $this->fieldMsg[$k]);
            }
            $this->data[$k] =  $request->$ks ?: $v;
        }
        if($this->data["type"] != '5' && empty($this->data["url"])){
            return ErrorCode::code("params_not_exist", $this->fieldMsg["url"]);
        }
        if($this->data["type"] == '1'){ // 1视频
            if(!VodVideoModel::query()->where("id", $this->data["url"])->count())
                return ErrorCode::code("customized", '视频不存在');
        }elseif($this->data["type"] == '2'){ // 2分区
            if(CatModel::getCount(["id" => $this->data["url"]]))
                return ErrorCode::code("customized", '分区不存在');
        }elseif($this->data["type"] == '3'){ // 3up主
            if(BroadcasterModel::getCount(["id" => $this->data["url"]]))
                return ErrorCode::code("customized", 'up主不存在');
        }
        $this->data["start_time"] = is_numeric($this->data["start_time"]) ? $this->data["start_time"] : strtotime($this->data["start_time"]);
        $this->data["end_time"] = is_numeric($this->data["end_time"]) ? $this->data["end_time"] : strtotime($this->data["end_time"]);
        CarouselModel::toAdd($this->data);
        return $this->return_result($this->returnData);
    }

    // 更新
    public function updateInfo(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        foreach ($this->field as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if($v === 'require' && !$request->$ks){
                return ErrorCode::code("params_not_exist", $this->fieldMsg[$k]);
            }
            if($request->$ks) {
                $this->data[$k] = trim($request->$ks);
            }
        }
        if($this->data["type"] != '5' && empty($this->data["url"])){
            return ErrorCode::code("params_not_exist", $this->fieldMsg["url"]);
        }
        if($this->data["type"] == '1'){ // 1视频
            if(!VodVideoModel::query()->where("id", $this->data["url"])->count())
                return ErrorCode::code("customized", '视频不存在');
        }elseif($this->data["type"] == '2'){ // 2分区
            if(CatModel::getCount(["id" => $this->data["url"]]))
                return ErrorCode::code("customized", '分区不存在');
        }elseif($this->data["type"] == '3'){ // 3up主
            if(BroadcasterModel::getCount(["id" => $this->data["url"]]))
                return ErrorCode::code("customized", 'up主不存在');
        }
        CarouselModel::doUpdate($this->data, ["id" => $request->id]);
        return $this->return_result($this->returnData);
    }

    // 获取信息
    public function getInfo(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $data = CarouselModel::getFields("*", ["id" => $request->id]);
        if(!empty($data)){
            $this->returnData["data"] = $data;
        }else{
            $this->returnData["data"] = [];
        }
        return $this->return_result($this->returnData);
    }

    // 删除
    public function delete(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        CarouselModel::delById($request->id);
        return $this->returnData;
    }
}
